//
//  UITextField+ERPIOS.m
//  ERPIOS
//
//  Created by Aaron on 15/12/2.
//  Copyright © 2015年 egeniuss. All rights reserved.
//

#import "UITextField+ERPIOS.h"
#import <objc/runtime.h>
@implementation UITextField (ERPIOS)

static const void * parameterKey = &parameterKey;
static const void * listKey = &listKey;
@dynamic parameter;
@dynamic alertList;
-(NSString*)leftMargin{
    return objc_getAssociatedObject(self, @selector(leftMargin));
}
-(void)setLeftMargin:(NSString*)leftMargin{
    objc_setAssociatedObject(self, @selector(leftMargin), leftMargin, OBJC_ASSOCIATION_COPY_NONATOMIC);
    if ([leftMargin floatValue]>0) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [leftMargin floatValue], 20)];
        [self setLeftViewWithView:view];
    }
}
-(NSString*)rightMargin{
    return objc_getAssociatedObject(self, @selector(rightMargin));
}
-(void)setRightMargin:(NSString*)rightMargin{
    objc_setAssociatedObject(self, @selector(rightMargin), rightMargin, OBJC_ASSOCIATION_COPY_NONATOMIC);
    if ([rightMargin floatValue]>0) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [rightMargin floatValue], 20)];
        [self setRightViewWithView:view];
    }
}
- (void)setParameter:(id)parameter{
    objc_setAssociatedObject(self, parameterKey, parameter, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (id)parameter{
    return objc_getAssociatedObject(self, parameterKey);
}

-(id)alertList{
     return objc_getAssociatedObject(self, listKey);
}
-(void)setAlertList:(NSArray *)alertList{
    objc_setAssociatedObject(self, listKey, alertList, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
-(void)setLeftSpaceWithSpace:(CGFloat)space{
    self.leftViewMode = UITextFieldViewModeAlways;
    UIView *lineview = [UIView new];
        lineview.backgroundColor = [UIColor clearColor];
        lineview.frame= CGRectMake(0, 0, space, self.frame.size.height);
    self.leftView = lineview;
}
-(void)setRightViewWithView:(UIView *)view{
    if (!view) {
        self.rightViewMode = UITextFieldViewModeNever;
        return;
    }
    self.rightViewMode = UITextFieldViewModeAlways;
    UIView *lineview = [UIView new];
        lineview.backgroundColor = [UIColor clearColor];
        lineview.frame= CGRectMake(0, 0, view.frame.size.width+8, self.frame.size.height);
    view.frame = CGRectMake(4, 4, view.frame.size.width, self.frame.size.height-8);
    [lineview addSubview:view];
    self.rightView = lineview;
}
-(void)setLeftViewWithView:(UIView *)view{
    if (!view) {
        self.rightViewMode = UITextFieldViewModeAlways;
        return;
    }
    self.leftViewMode = UITextFieldViewModeAlways;
    UIView *lineview = [UIView new];
        lineview.backgroundColor = [UIColor clearColor];
        lineview.frame= CGRectMake(0, 0, view.frame.size.width+8, self.frame.size.height);
    view.frame = CGRectMake(4, 4, view.frame.size.width, self.frame.size.height-8);
    [lineview addSubview:view];
    self.leftView = lineview;
}
@end
